#pragma once

#include "VisioCommon.h"
#include <opencv2\opencv.hpp>
#include "TrackData.h"
#include <vector>

#define UNORM_TO_FLOAT(_x) (float(_x) / 255.0f)
#define FLOAT_TO_UNORM(_f) (int(_f * 255.0f))
struct KeyColor
{
	KeyColor() {}
	KeyColor(float _r, float _g, float _b) : r(_r), g(_g), b(_b) {}
	float r,g,b;
};

struct KeyPoint2D
{
	KeyPoint2D(double x, double y, int _r, int _g, int _b) : KeyPoint(x, y), Mark(-1), 
															color((UNORM_TO_FLOAT(_r)), (UNORM_TO_FLOAT(_g)), (UNORM_TO_FLOAT(_b))) {}
	cv::Point2d KeyPoint;
	KeyColor color;
	int Mark;
};
typedef std::vector<KeyPoint2D> KeyPointVector;

//(pTrack, KeyPointIndex) 
typedef std::pair<CTrackData*, UINT> VisablePoint;
typedef cv::Mat cvImage;
typedef std::vector<VisablePoint> VisiblePointsVector;
typedef VisiblePointsVector::iterator VisablePointItr;

class CImageData
{
public:
	CImageData(KeyPointVector points, const char* ImageName, UINT index, double _focal);
	void SetVisablePoint(CTrackData* pTrack, UINT KeyPointIndex);

	//iterators for visable points
	UINT GetNumVisablePoints() const;
	VisablePointItr VisablePointsBegin(); 
	VisablePointItr VisablePointsEnd();

	//return the index of the image which this image was created with
	UINT GetImageIndex() const;

	//checks whether the image sees the track in ItrToFind
	VisablePointItr FindVisablePoint(VisablePointItr ItrToFind);

	//return the actuall image
	cvImage GetImage() const;
	double GetFocal() const;
	cv::Point2d GetKeyPoint(UINT index) const;
	void MarkKeyPoint(UINT KeyPointIndex, int mark);
	int GetMark( UINT KeyPointIndex ) const;
	void RemoveMark( UINT KeyPointIndex );
	UINT GetNumKeyPoints() const;
	bool IsKeyPointMarked( UINT KeyPointIndex ) const;
	string GetName() const;
	KeyColor GetKeyPointColor(UINT index) const;
private:
	//holds the data which tracks and their corresponding KeyPoints the image sees
	VisiblePointsVector m_VisablePoints;

	KeyPointVector m_keys;
	cvImage m_Image;
	string m_ImageName;
	bool m_bTracksLoaded;
	bool m_bKeysLoaded;
	double m_Focal;
	UINT m_index;
};